import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询备件备料单'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备件备料单详情'
});
const exportDesc = formatMessage({
    id: 'api.message.exportDesc',
    defaultMessage: '导出备件备料单'
});
const deliveryDesc = formatMessage({
    id: 'api.message.deliveryDesc',
    defaultMessage: '发货'
});
const deliveryOrderDesc = formatMessage({
    id: 'api.message.deliveryOrderDesc',
    defaultMessage: '查询交货单'
});
const getDealersDesc = formatMessage({
    id: 'api.message.getDealersDesc',
    defaultMessage: '查询总代信息'
});
const postingDesc = formatMessage({
    id: 'api.message.postingDesc',
    defaultMessage: '发货过账'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/picking/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getListDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/pickings?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/pickings/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//发货
export const delivery = data => request(`${baseUrl}/pickings/delivery`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, deliveryDesc));
//发货过账
export const posting = data => request(`${baseUrl}/pickings/posting`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, postingDesc));
// 导出
export const exportPartIns = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/pickings/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDesc))
        .then(res => res.data);
};
//发货查询交货单查询
export const deliveryOrderQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/deliveryOrders/forPicking?${queryString}`, null, dto.DeliveryList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, deliveryOrderDesc));
};
//发货过账查询交货单
export const deliveryOrderPostQuery = pageOption => {
    const queryString = stringify({
        ...pageOption,
        isDelivery: false
    });
    return request(`${baseUrl}/deliveryOrders/forPicking?${queryString}`, null, dto.DeliveryList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, deliveryOrderDesc));
};
// 快捷查询总代
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer/simple?${queryString}`, null, dto.DealerSimpleList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, dto.DealerList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
